﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.EntityQuery;
using PMS.Common;
using System.Data.SqlClient;

public partial class Login : Page
{

    ISys_UserHandler userHandler = new Sys_UserManage();
    ISys_RoleHandler roleHandler = new Sys_RoleManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["LoginID"] != null)
            {
                Response.Redirect("~/Modules/Default/Default.aspx");
            }
        }     
    }  


    protected void btnLogin_Click(object sender, EventArgs e)
    {
        bool isLoginValid = false;
        if (Session["LoginValid"] != null)
        {
            if (Session["LoginValid"].ToString().Trim() == txtValidCode.Text.Trim())
            {
                isLoginValid = true;
            }
        }

        txtValidCode.Text = string.Empty;

        if (!isLoginValid)
        {
            Jscript.Alert("验证码输入错误,请重新输入!", this.Page);
            return;
        }

        SqlParameter[] parms = new SqlParameter[2];
        parms[0] = new SqlParameter("@Code", txtName.Text);
        parms[1] = new SqlParameter("@Pwd", DesEncryption.EncryptPassword(txtPassword.Text, "MD5"));

        DataTable dt = userHandler.GetSys_UserList(" Code=@Code and Pwd=@Pwd and Status=" + Enums.NormalStatus.Vallid.ToString("d"), parms);

        if (dt != null && dt.Rows.Count > 0)
        {
            Session["CurrentUserID"] = int.Parse(dt.Rows[0]["ID"].ToString());
            Session["CurrentUserName"] = dt.Rows[0]["Name"].ToString();
            Session["CurrentLoginId"] = dt.Rows[0]["Code"].ToString();
            Session["UserPurviews"] = GetPurviewsByRoles(dt.Rows[0]["Roles"].ToString());
            Session["OrgID"] = dt.Rows[0]["Org_ID"].ToString();
            Session["OrgID2"] = dt.Rows[0]["Org_ID2"].ToString();
            Session["PostID"] = dt.Rows[0]["Post_ID"].ToString();
            Session["PostID2"] = dt.Rows[0]["Post_ID2"].ToString();

            if (Request["returnUrl"] != null)
            {
                Response.Redirect(Request["returnUrl"]);
            }
            else
            {
                Response.Redirect("~/Modules/Default/Default.aspx");
            }
        }
        else
        {

            Jscript.Alert("用户或密码错误,请重新输入!", this.Page);


            txtPassword.Focus();
        }
    }


    protected void btnClose_Click(object sender, EventArgs e)
    {
        txtName.Text = string.Empty;
        txtPassword.Text = string.Empty;
    }


    private string GetPurviewsByRoles(string roles)
    {
        string purStr = string.Empty;
        DataTable dtRoles = roleHandler.GetSys_RoleList(" Id in(" + roles + ")", null);
        if (dtRoles != null && dtRoles.Rows.Count > 0)
        {
            for (int i = 0; i < dtRoles.Rows.Count; i++)
            {
                purStr += dtRoles.Rows[i]["Purviews"].ToString() + ",";
            }
            purStr = purStr.TrimEnd(new char[] { ',' });
        }
        //Console.WriteLine(purStr);
        return purStr;
    }
}
